package com.kaikai.edu.listener;

import com.kaikai.commonutils.JsonUtil;
import com.kaikai.config.ChannelConstant;
import com.kaikai.edu.channel.EmailMsgChannel;
import com.kaikai.edu.entity.EduEmail;
import com.kaikai.edu.mapper.EmailMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @Author yujiankai
 * @Description 邮件回执监听
 * @date 2022/1/18 17:17
 */
@EnableBinding(value = EmailMsgChannel.class)
@Component
public class EmailReceiptListener {

    private static Logger logger = LoggerFactory.getLogger(EmailReceiptListener.class);

    @Autowired
    private EmailMapper emailMapper;


    @StreamListener(ChannelConstant.EMAIL_RECEIPT_CHANNEL)
    public void receiveInnerMsgRtn(Message<String> message){
        try {
            EduEmail eduEmail = JsonUtil.convertJsonToObject(message.getPayload(), EduEmail.class);
            logger.info("--消息回执--"+message.getPayload());
            //更新消息记录
            emailMapper.updateEmailSendRec(eduEmail);
        }catch(Exception e){
            logger.error("回执失败"+e);
        }
    }
}
